/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ucm;

import controller.Traffic;
import java.util.zip.DataFormatException;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import model.Const;
import model.IntersectionInfo;
import view.Display;

/**
 * Use case control components class to handle run time environment information button event.
 * @author Chiaki Sato
 * @since J2SDK 1.6
 * @version 1.0
 */
public class UCEnvironmentInformationButton extends JButton implements UCController {

    private IntersectionInfo oIntersectionInfo;
    private Traffic oTraffic;
    private Display oDisplay;

    /**
     * Constructor of environment information button class that initializes instance variables based on input parameters.
     * @param stText Control caption text
     * @param oIntersectionInfo Intersection information object
     * @param oDisplay Display object
     * @param oTraffic Traffic object
     */
    public UCEnvironmentInformationButton(String stText, IntersectionInfo oIntersectionInfo, Display oDisplay, Traffic oTraffic){
        super(stText);
        this.oIntersectionInfo = oIntersectionInfo;
        this.oDisplay = oDisplay;
        this.oTraffic = oTraffic;
    }

    /**
     * Public run function to execute a process of the environment information button.
     */
    public void run() {

        // User clicks the set button
        try {
            // System gets the total number of horizontal street lanes from the text field
            int nLaneHoriz = oDisplay.getLanesHoriz();

            // System gets the total number of vertical street lanes from the text field
            int nLaneVert = oDisplay.getLanesVert();

            // System gets the horizontal probability of a car entering horizontal street from the text field
            float fProbabilityHoriz = oDisplay.getProbabilityHoriz();

            // System gets the horizontal probability of a car entering vertical street from the text field
            float fProbabilityVert = oDisplay.getProbabilityVert();

            // System sets the total number of horizontal street lanes into the simulator run time environment information
            oIntersectionInfo.setLaneHoriz(nLaneHoriz);

            // System System sets the total number of vertical street lanes into the simulator run time environment information
            oIntersectionInfo.setLaneVert(nLaneVert);

            // System sets the horizontal probability of a car entering horizontal street into the simulator run time environment information
            oIntersectionInfo.setProbabilityHoriz(fProbabilityHoriz);

            // System sets the vertical probability of a car entering vertical street into the simulator run time environment information
            oIntersectionInfo.setProbabilityVert(fProbabilityVert);

            // System ends current traffic simulation
            oIntersectionInfo.setTafficOnGoing(false);

            // System resets traffic simulation controller using new run time environment values
            oTraffic.initTraffic(oIntersectionInfo);

            // System resets the simulation display using new run time environment values
            oDisplay.setIntersectionInfo(oIntersectionInfo);
            oDisplay.setTraffic(oTraffic);

            // System redraws the simulation display using new run time environment values
            oDisplay.repaint();

        }
        catch (DataFormatException exp){

            // System catches that the input value is not a valid number or value
            // System stops to change the run time environment and
            // System shows an error message
            JOptionPane.showMessageDialog(null, exp, Const.ERR_TITLE_IN_CLASS + getClass().getName(), JOptionPane.ERROR_MESSAGE);
        }
        catch (Exception exp){

            JOptionPane.showMessageDialog(null, exp, Const.ERR_TITLE_IN_CLASS + getClass().getName(), JOptionPane.ERROR_MESSAGE);
        }
    }
}
